# vim: set filetype=py

def getRevision(env):
	try:
		import bzrlib
		import bzrlib.builtins

		tree = bzrlib.builtins.WorkingTree.open_containing(env.Dir("#").abspath)[0]

		return tree.branch.revision_id_to_revno(tree.last_revision())

	except:
		f = None
		try:
			f = open(env.Dir("#").abspath+"/.bzr/branch/last-revision",'r')
			line = f.read()
			pos = line.find(' ')
			if pos > 0:
				return int(line[:pos])
		except:
			pass
		finally:
			if f:
				f.close()
		return 0

Import('dev source_path')

env, target, sources = dev.prepare_build(source_path, 'dcpp', in_bin = False, precompiled_header = 'stdinc')

env.Append(CPPPATH = ['#/openssl/include', '#/bzip2', '#/zlib'])

env.Append(CPPDEFINES=["BUILDING_DCPP=1"])

for i, source in enumerate(sources):
	if source.find("version.cpp") != -1:
		rev = ['DCPP_REVISION=' + str(getRevision(env))]
		sources[i] = env.StaticObject(source, CPPDEFINES=env['CPPDEFINES'] + rev)

headers=dev.get_sources(source_path, "*.h")

dev.i18n(source_path, env, [sources, headers], 'libdcpp')

ret = env.StaticLibrary(target, sources)

Return('ret')
